package com.qt.thread.homework.work3;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * @Author zsf
 * @Date 2023/10/4 13:40
 * @Version 1.0
 */
public class LotteryBox implements Callable<HashMap> {

    private List<Integer> arr;
    private List<Integer> result = new ArrayList<>();
    private int sumMoney = 0;
    private HashMap resultMap = new HashMap<>();
    private String name;

    public LotteryBox(List<Integer> arr) {
        this.arr = arr;
    }

    @Override
    public HashMap call() throws Exception {
        resultMap.put("list",result);

        resultMap.put("name",Thread.currentThread().getName());
        while (true){
            synchronized (LotteryBox.class) {
                if(result.size() < 5){
                    Collections.shuffle(arr);
                    Integer item = arr.get(0);
                    result.add(item);
                    sumMoney += item;
                    arr.remove(0);
                }else {
                    resultMap.put("sum",sumMoney);
                    break;
                }
            }
        }

        return resultMap;
    }
}
